DeathwatchGuidesWindow = {}

DeathwatchGuidesWindow.data = {} 
DeathwatchGuidesWindow.orderTable = {}
DeathwatchGuidesWindow.listBoxData = {} 
DeathwatchGuidesWindow.rowToEntryMap = {} 
DeathwatchGuidesWindow.numTotalRows = 0 
DeathwatchGuidesWindow.lastPressedButtonId = 0
DeathwatchGuidesWindow.AlphaValue = 1

function print(txt)
	ChatWindow.Print(towstring(txt))
end

function DeathwatchGuidesWindow.Initialize()
    LabelSetText( "DeathwatchGuidesWindowTitleBarText", L"Deathwatch Guides" )
    ButtonSetText("DeathwatchGuidesWindowBackButton", GetHelpString( StringTables.Help.BUTTON_BACK))
    DeathwatchGuidesWindow.SetGuide(1)
end

function DeathwatchGuidesWindow.SetGuide(guide)
    DeathwatchGuidesWindow.data = DeathwatchGuidesWindow.getHeader(guide)
    DeathwatchGuidesWindow.PrepareData()
    DeathwatchGuidesWindow.UpdateDeathwatchGuidesWindowRow()

    LabelSetText( "DeathwatchGuidesWindowHeaderText", V["G_GUIDE_".. guide ] )
    LabelSetText( "DeathwatchGuidesWindowTopicLabel", GetHelpString(StringTables.Help.LABEL_CHOOSE_A_TOPIC) )
    LabelSetText( "DeathwatchGuidesWindowMainText", L"" )
    DeathwatchGuidesWindow.SetListRowTints()
    if( DeathwatchGuidesWindow.data ) then
	DeathwatchGuidesWindow.DisplayRow( 1 )
	DeathwatchGuidesWindow.DisplayRow( 2 )
    end
end

function DeathwatchGuidesWindow.OnShown()
end

function DeathwatchGuidesWindow.ToggleShowing()
end

function DeathwatchGuidesWindow.Show()
    WindowSetShowing("DeathwatchGuidesWindow", true)
end

function DeathwatchGuidesWindow.Hide()
    WindowSetShowing("DeathwatchGuidesWindow", false)
end

function DeathwatchGuidesWindow.Shutdown()
end

function DeathwatchGuidesWindow.UpdateDeathwatchGuidesWindowRow()
end

function DeathwatchGuidesWindow.OnRButtonUpPlayerRow()
end

function DeathwatchGuidesWindow.MouseOverRow()
    local row = WindowGetId( SystemData.ActiveWindow.name )
    local targetRowWindow = "DeathwatchGuidesWindowPlayerListRow"..row
    DefaultColor.SetLabelColor(targetRowWindow.."Name", DefaultColor.MAGENTA)
end

function DeathwatchGuidesWindow.PrepareData()
    DeathwatchGuidesWindow.orderTable = {}
    DeathwatchGuidesWindow.listBoxData = {}
    DeathwatchGuidesWindow.numTotalRows = 1
    DeathwatchGuidesWindow.rowToEntryMap = {}
    
    if( not DeathwatchGuidesWindow.data ) then return end
    
    for sectionIndex, sectionData in ipairs( DeathwatchGuidesWindow.data ) do 
        local sectionTable = {name = sectionData.name, isSection = true, id = sectionData.id}
        table.insert( DeathwatchGuidesWindow.listBoxData, DeathwatchGuidesWindow.numTotalRows, sectionTable )
        table.insert( DeathwatchGuidesWindow.orderTable, DeathwatchGuidesWindow.numTotalRows )
        local indexStruct = {index = sectionIndex, isSection = true}
        table.insert( DeathwatchGuidesWindow.rowToEntryMap, DeathwatchGuidesWindow.numTotalRows, indexStruct )
        DeathwatchGuidesWindow.numTotalRows = DeathwatchGuidesWindow.numTotalRows + 1
        if( sectionData.expanded == true ) then
            for entryIndex, entryData in ipairs( sectionData.entries ) do
                local entryTable = {name = L"   ".. entryData.id..L") "..entryData.name, isSection = false, id = entryData.id}
                table.insert( DeathwatchGuidesWindow.listBoxData, DeathwatchGuidesWindow.numTotalRows, entryTable )
                table.insert(DeathwatchGuidesWindow.orderTable, DeathwatchGuidesWindow.numTotalRows)
                local indexStruct = {categoryIndex = sectionIndex, index = entryIndex, isSection = false}
                table.insert( DeathwatchGuidesWindow.rowToEntryMap, DeathwatchGuidesWindow.numTotalRows, indexStruct )
                DeathwatchGuidesWindow.numTotalRows = DeathwatchGuidesWindow.numTotalRows + 1
            end
        end
    end
    
    ListBoxSetDisplayOrder("DeathwatchGuidesWindowPlayerList", DeathwatchGuidesWindow.orderTable )
    DeathwatchGuidesWindow.SetListRowTints()
    DeathwatchGuidesWindow.UpdateColorsAndSigns()
    DeathwatchGuidesWindow.ResetAllButtons()
end

function DeathwatchGuidesWindow.SetListRowTints()
    for row = 1, DeathwatchGuidesWindow.numTotalRows do
        local row_mod = math.mod(row, 2)
        color = DataUtils.GetAlternatingRowColor( row_mod )

        local targetRowWindow = "DeathwatchGuidesWindowPlayerListRow"..row
        WindowSetTintColor(targetRowWindow.."RowBackground", color.r, color.g, color.b )
        WindowSetAlpha(targetRowWindow.."RowBackground", color.a )
    end
end

function DeathwatchGuidesWindow.OnLButtonUpPlayerRow()
    local row = WindowGetId( SystemData.ActiveWindow.name )
    DeathwatchGuidesWindow.DisplayRow(ListBoxGetDataIndex("DeathwatchGuidesWindowPlayerList", row))
    DeathwatchGuidesWindow.UpdateColorsAndSigns()
end

function DeathwatchGuidesWindow.ResetPressedButton()
    DeathwatchGuidesWindow.SetEntrySelectedById( DeathwatchGuidesWindow.lastPressedButtonId, false )
end

function DeathwatchGuidesWindow.ResetAllButtons()
    for index, data in ipairs( DeathwatchGuidesWindow.listBoxData ) do
        if( DeathwatchGuidesWindow.lastPressedButtonId == data.id and data.isSection == false ) then
            ButtonSetPressedFlag("DeathwatchGuidesWindowPlayerListRow"..index.."Name", true )
        else
            ButtonSetPressedFlag("DeathwatchGuidesWindowPlayerListRow"..index.."Name", false )
        end
    end
end

function DeathwatchGuidesWindow.DisplayRow( row )
    local index = DeathwatchGuidesWindow.rowToEntryMap[row].index
    if( DeathwatchGuidesWindow.rowToEntryMap[row].isSection ) then
        if( DeathwatchGuidesWindow.data[index].expanded ) then
            DeathwatchGuidesWindow.data[index].expanded = false
        else
            DeathwatchGuidesWindow.data[index].expanded = true
        end
    else
        DeathwatchGuidesWindow.ResetPressedButton()
        local categoryIndex = DeathwatchGuidesWindow.rowToEntryMap[row].categoryIndex
        DeathwatchGuidesWindow.lastPressedButtonId = DeathwatchGuidesWindow.data[categoryIndex].entries[index].id
        ButtonSetPressedFlag("DeathwatchGuidesWindowPlayerListRow"..row.."Name", true )
        DeathwatchGuidesWindow.DisplayManualEntry( DeathwatchGuidesWindow.data[categoryIndex].entries[index].name, DeathwatchGuidesWindow.data[categoryIndex].entries[index].text ) -- display text for the just selected entry

    end
        
    DeathwatchGuidesWindow.PrepareData()
end

function DeathwatchGuidesWindow.SetEntrySelectedById( id, selected )
    for index, data in pairs( DeathwatchGuidesWindow.listBoxData ) do
        if( data.isSection == false and data.id == DeathwatchGuidesWindow.lastPressedButtonId ) then
            ButtonSetPressedFlag("DeathwatchGuidesWindowPlayerListRow"..index.."Name", selected )
        end
    end
end

function DeathwatchGuidesWindow.DisplayManualEntry( name, text )
    LabelSetText( "DeathwatchGuidesWindowTopicLabel", towstring(name) ) 
    LabelSetText( "DeathwatchGuidesWindowMainText", towstring(text) )
    ScrollWindowSetOffset( "DeathwatchGuidesWindowMain", 0 )
    ScrollWindowUpdateScrollRect("DeathwatchGuidesWindowMain")
end

function DeathwatchGuidesWindow.OnLButtonUpBackButton()
	DeathwatchGuidesWindow.Hide()
	DeathwatchGuides_Hotspot.CheckSelection = 0
	WindowSetShowing("DeathwatchGuides_ScrollSelectGuide", true)
end

function DeathwatchGuidesWindow.UpdateColorsAndSigns()
    for index = 1, DeathwatchGuidesWindowPlayerList.numVisibleRows do
        WindowSetShowing( "DeathwatchGuidesWindowPlayerListRow"..index.."MinusButton", false )
        WindowSetShowing( "DeathwatchGuidesWindowPlayerListRow"..index.."PlusButton", false )
    end
end

function DeathwatchGuidesWindow.getHeader(guide)
	obj = {}
	data={}

	chapter_amount = V["CHAPTER_AMOUNT_" .. guide]
	for i=1, chapter_amount do
		data[i] = {
			id = i,
			expanded = false,
			name = "Temp Chap ",
			entries = {
				id = 0,
				name = "Temp Sub Name",
				text = "Temp Text"
			}
		}
		data[i].id = i
		data[i].name = V["CHAPTER_"..guide.."_"..i]
		data[i].expanded = false
		title_amount = V["TITLE_AMOUNT_"..guide.."_"..i]
		for j=1,title_amount do
			data[i].entries[j] = {}
			data[i].entries[j].id = j
			data[i].entries[j].name = V["TITLE_"..guide.."_"..i.."_"..j]
			data[i].entries[j].text = V["TITLE_DESC_"..guide.."_"..i.."_"..j]
			table.insert(data[i], data[i].entries[j])
		end
			
	table.insert(obj,data[i])
	end
	return obj
end

function DeathwatchGuidesWindow.Init()
	LibSlash.RegisterSlashCmd("kg", function(arg) DeathwatchGuidesWindow.SlashHandler(arg) end)
	LibSlash.RegisterSlashCmd("Deathwatchguide", function(arg) DeathwatchGuidesWindow.SlashHandler(arg) end)
end

function DeathwatchGuidesWindow.SlashHandler(arg)
	local opt, val = args:match("([a-z0-9]+)[ ]?(.*)")

	if not opt then

	elseif opt == "on" then
		WindowSetShowing("DeathwatchGuides_ScrollSelectGuide", true)

	elseif opt == "off" then
		WindowSetShowing("DeathwatchGuides_ScrollSelectGuide", false)
	else
		ChatWindow.Print(L"Invalid option. Do /kg for a list of valid ones.")
	end
end

function DeathwatchGuidesWindow.OnLButtonUpMinusButton()
	if DeathwatchGuidesWindow.AlphaValue > 0 then
		alpha = DeathwatchGuidesWindow.AlphaValue - 0.2
		WindowSetAlpha("DeathwatchGuidesWindowBackground", alpha)
		DeathwatchGuidesWindow.AlphaValue = alpha
	end
end

function DeathwatchGuidesWindow.OnLButtonUpPlusButton()
	if DeathwatchGuidesWindow.AlphaValue < 1 then
		alpha = DeathwatchGuidesWindow.AlphaValue + 0.2
		WindowSetAlpha("DeathwatchGuidesWindowBackground", alpha)
		DeathwatchGuidesWindow.AlphaValue = alpha
	end
end

